﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using Microsoft.SharePoint;
using Microsoft.Office.Server.UserProfiles;

namespace Ko2.Portal.SP_List
{

    public class SPList_BirthdaysAll
    {

        public Exception Select(ref DataTable tbl)
        {
            Exception ret = null;
            DataTable tblTmp = new DataTable("tbl");
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {

                using (SPSite site = new SPSite(@"http://localhost/"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists[@"BirthdaysAll"];
                        SPQuery q = new SPQuery();
                        q.Query =
@"<Query>
   <OrderBy>
      <FieldRef Name='IsOffice' />
      <FieldRef Name='OfficeName' />
      <FieldRef Name='UserName' />
   </OrderBy>
</Query>";
                        SPListItemCollection items = list.GetItems(q);
                        tblTmp = items.GetDataTable();
                    }
                }
            });
            if (tblTmp != null)
                tbl = tblTmp;
            else
                tbl = new DataTable("tbl");
            return ret;
        }

        public Exception SelectFromWS(ref DataTable tbl, ref List<string> errList)
        {
            return new Ko2.Web.Utils.WS_Helper().GetSPUsersBirthdaysAll(ref tbl, ref errList);
        }

        public Exception Insert(DateTime dt, UserProfile user, bool IsOffice, string EMail, string Img, string OfficeName, string DeparmentName, string jobName, string Phone)
        {
            Exception ret = null;
            string Login = "";
            string Mem = "";
            string DisplayName = "";
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite("http://localhost/"))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPList list = web.Lists["BirthdaysAll"];
                            SPListItem item = list.Items.Add();
                            string UserProfile_GUID = "";
                            string WebSite = "";
                            string UserName = "";
                            if (user != null)
                            {
                                Login = (string)user["AccountName"].Value;
                                
                                Mem = (string)user["AboutMe"].Value;
                                if (Mem == null)
                                    Mem = "";
                                
                                DisplayName = (string)user["PreferredName"].Value;
                                if (DisplayName == null)
                                    DisplayName = "";
                                
                                UserProfile_GUID = (string)user["UserProfile_GUID"].Value.ToString();
                                if (user["UserProfile_GUID"].Value == null || String.IsNullOrEmpty(UserProfile_GUID))
                                    UserProfile_GUID = "";

                                WebSite = (string)user["PersonalSpace"].Value;
                                if (String.IsNullOrEmpty(WebSite))
                                    WebSite = "";

                                UserName = (string)user["UserName"].Value;
                                if (String.IsNullOrEmpty(UserName))
                                    UserName = "";
                            }
                            string JobName = "";
                            if (!String.IsNullOrEmpty(jobName))
                                JobName = jobName;

                            item["Title"] = DisplayName;
                            item["DT"] = dt;
                            SPUser spuser = null;
                            try
                            {
                                spuser = web.SiteUsers[user["AccountName"].ToString()];
                            }
                            catch
                            {
                            }
                            item["SPUser"] = spuser;
                            item["IsOffice"] = IsOffice;
                            item["Login"] = Login;
                            item["Img"] = Img;
                            item["EMail"] = EMail;
                            item["OfficeName"] = OfficeName;
                            item["DepartmentName"] = DeparmentName;
                            item["jobName"] = JobName;
                            item["UserProfile_GUID"] = UserProfile_GUID;
                            item["WebSite"] = WebSite;
                            item["UserName"] = UserName;
                            item["Phone"] = Phone;

                            item.Update();
                        }
                    }
                });
            }            
            catch (Exception err)
            {
                ret = new Exception(err.Message, err);
            }
            return ret;
        }

        public Exception DeleteAll()
        {
            Exception ret = null;
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite("http://localhost/"))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPList list = web.Lists["BirthdaysAll"];
                            int iCount = list.Items.Count;
                            for (int i = list.Items.Count - 1; i >= 0; i--)
                                list.Items[i].Delete();
                            list.Update();
                        }
                    }
                });
            }
            catch (Exception err)
            {
                ret = new Exception(err.Message, err);
            }
            return ret;
        }

    }

}
